package com.example.libo.huihui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.libo.huihui.BR;
import com.example.libo.huihui.R;
import com.example.libo.huihui.adapter.BindAdapter;
import com.example.libo.huihui.application.MyApplication;
import com.example.libo.huihui.been.SearchListEntity;
import com.example.libo.huihui.databinding.ActivitySearchBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SearchActivity extends AppCompatActivity implements Callback<SearchListEntity> {
    private ActivitySearchBinding binding;
    private BindAdapter adapter;
    private ArrayList<SearchListEntity.DataBean> datas = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(SearchActivity.this, R.layout.activity_search);
        init();
        event();
    }

    private void init() {
        Map<Class<?>,Pair<Integer,Integer>> map = new HashMap<>();
        map.put(SearchListEntity.DataBean.class,Pair.create(R.layout.item_searchlist,BR.searchlist));
        adapter = new BindAdapter(map,datas,SearchActivity.this);
        binding.rvList.setLayoutManager(new GridLayoutManager(getApplicationContext(),4));
        binding.rvList.setAdapter(adapter);
        MyApplication.getHttpService().getSearchList().enqueue(this);
    }

    private void event(){
        binding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String keyWord = binding.etSearchbox.getText().toString();
                gotoSearch(keyWord);
            }
        });
        adapter.setOnItemClickListener(new BindAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                String keyWord = datas.get(position).getName();
                gotoSearch(keyWord);
            }
        });
    }

    private void gotoSearch(String keyWord){
        if(null != keyWord && keyWord.length() != 0){
            Intent intent = new Intent(SearchActivity.this,SearchResultActivity.class);
            intent.putExtra("key",keyWord);
            startActivity(intent);
        }else{
            Toast.makeText(SearchActivity.this,"请输入搜索内容",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onResponse(Call<SearchListEntity> call, Response<SearchListEntity> response) {
        datas.addAll(response.body().getData());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<SearchListEntity> call, Throwable t) {
    }
}
